﻿Public Class xtf_ABMlinea

    Dim ulg_row As DataRow
    Dim obj_linea_ As cls_linea_ = Nothing
    Dim obj_obra As cls_centrocostoorden_

    Public Sub New(ByVal obra As cls_centrocostoorden_)
        InitializeComponent()
        obj_obra = obra
        Cargarsistema_()
        Cargarmaterial_()
        AgregarValidaciones()
    End Sub

    Public Overrides Sub CargarObjetos()
        Cargarsistema_()
        Cargarmaterial_()
    End Sub


    Private Sub Cargarmaterial_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_material.EditValue

        Dim material_ As New cls_material_
        material_.CargarTodos()
        cargarCombo(cbo_material, material_.Items, False, True)
        cbo_material.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_material.EditValue)
    End Sub

    Private Sub Cargarsistema_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_sistema.EditValue

        Dim sistema_ As New cls_sistema_
        sistema_.CargarTodos()
        cargarCombo(cbo_sistema, sistema_.Items, False, True)
        cbo_sistema.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_sistema.EditValue)
    End Sub

    Private Sub Cargarsubsistema_()
        Dim tmp_obj As Integer = 0
        tmp_obj = cbo_subsistema.EditValue

        Dim subsistema_ As New cls_subsistema_
        subsistema_.CargarXidsistema(cbo_sistema.EditValue)
        cargarCombo(cbo_subsistema, subsistema_.Items, False, True)
        cbo_subsistema.EditValue = IIf(tmp_obj <> 0, tmp_obj, cbo_subsistema.EditValue)
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            'Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            Me.Text = "Línea - Nuevo"
            obj_linea_ = New cls_linea_
            btn_Registro.Enabled = False
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef linea As cls_linea_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_linea_) Then
            obj_linea_ = New cls_linea_
        End If
        obj_linea_ = linea
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        'Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_linea_.ToString
        Me.Text = "Línea - " & obj_linea_.ToString
        txt_ID.Text = obj_linea_.idlinea
        txt_Nombre.Text = obj_linea_.nombre
        spe_alcance.EditValue = obj_linea_.alcance
        cbo_sistema.EditValue = obj_linea_.idsistema
        cbo_subsistema.EditValue = obj_linea_.idsubsistema
        cbo_material.EditValue = obj_linea_.idmaterial

        btn_Registro.Enabled = True

        Me.Show()

    End Sub

    Public Overrides Function ComprobarDatos() As Boolean
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If

        If txt_Nombre.Text = "" Then
            ErrManagerShow("001038", Me)
            Return False
        End If

        'REGISTROS DUPLICADOS----------------------------------------------------
        Dim col As New Collections.Generic.List(Of stc_campoValor)
        Dim comp1 As New stc_campoValor
        comp1.campo = "nombre"
        comp1.valor = txt_Nombre.Text
        col.Add(comp1)
        If ComprobarDuplicado("linea_", col, "idlinea <> " & IIf(txt_ID.Text = "", 0, txt_ID.Text)) Then
            ErrManagerShow("000010", Me, " Dato: Nombre")
            Return False
        End If
        'REGISTROS DUPLICADOS----------------------------------------------------

        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_linea_.nombre = txt_Nombre.Text
            obj_linea_.alcance = spe_alcance.EditValue
            obj_linea_.idsistema = cbo_sistema.EditValue
            obj_linea_.idsubsistema = cbo_subsistema.EditValue
            obj_linea_.idmaterial = cbo_material.EditValue
            obj_linea_.idobra = obj_obra.idcentrocostoorden
            If txt_ID.Text = "" Then
                obj_linea_.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_ABMsoldadura_).AgregarItem(obj_linea_)
                End If
                Modificar(obj_linea_, frm_listado, ulg_row)
            Else
                obj_linea_.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_ABMsoldadura_).ModificarItem(obj_linea_, ulg_row)
                End If
                Modificar(obj_linea_, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_linea_.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_linea_.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub cbo_sistema_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbo_sistema.EditValueChanged
        Cargarsubsistema_()
    End Sub

End Class